package com.cskaoyan.day2.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author Common-zhou
 * @since 2023-02-28 11:28
 */
public class CollectionDemo6 {
    public static void main(String[] args) {
        //testIterator();

        //testIterator2();

        //testIterator3();

        testIterator4();
    }

    private static void testIterator4() {
        Collection<String> collection = new ArrayList<>();

        collection.add("zs");
        collection.add("ls");
        collection.add("wu");

        Iterator<String> iterator = collection.iterator();

        //   zs    ls    wu
        //                  |
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

    private static void testIterator3() {
        Collection<String> collection = new ArrayList<>();

        collection.add("zs");
        collection.add("ls");
        collection.add("wu");

        //     zs    ls     wu
        //   |

        Iterator<String> iterator = collection.iterator();
        System.out.println(iterator.hasNext());
        System.out.println(iterator.hasNext());

        //     zs    ls     wu
        //   |

        System.out.println(iterator.next());

        //     zs    ls     wu
        //        |

        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        //     zs    ls     wu
        //              |

        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        //     zs    ls     wu
        //                      |

        System.out.println(iterator.hasNext());
        //     zs    ls     wu
        //                      |

    }

    private static void testIterator2() {
        Collection<String> collection = new ArrayList<>();

        collection.add("zs");
        collection.add("ls");
        collection.add("wu");


        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    private static void testIterator() {
        Collection<String> collection = new ArrayList<>();

        collection.add("zs");
        collection.add("ls");
        collection.add("wu");

        // 为什么返回了一个接口？
        Iterator<String> iterator = collection.iterator();

        // 是否有下一个元素
        //boolean b = iterator.hasNext();
        //
        //System.out.println(b);
        //
        //// 返回下一个元素
        //String next = iterator.next();
        //System.out.println(next);

        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());

        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());

        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());

        System.out.println(iterator.hasNext());
        // NoSuchElementException
        //System.out.println(iterator.next());
    }
}
